package yaner.hotel.manager.resource.controller;

import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import yaner.hotel.manager.common.dto.LoginDto;
import yaner.hotel.manager.common.enums.ResponseCodeEnums;
import yaner.hotel.manager.common.utils.SnowUtils;
import yaner.hotel.manager.common.vo.LoginVo;
import yaner.hotel.manager.common.vo.ResponseVo;
import yaner.hotel.manager.common.vo.UserVo;
import yaner.hotel.manager.resource.service.IUserService;

import java.util.List;

/**
 * @Author kf-leig
 * @Description: 登录
 * @Version 1.0
 * @Date 2024年05月22日11:31:00
 */
@RestController
@Slf4j
public class LoginController {
    private final IUserService userService;
    public LoginController(IUserService userService) {
        this.userService = userService;
    }

    @RequestMapping("login")
    public ResponseVo<LoginVo> login(@RequestBody LoginDto loginDto) {
        List<UserVo> userVoList = userService.checkUserData(loginDto.getLoginAccount());
        if (userVoList.size() > 1) {
            return ResponseVo.error(ResponseCodeEnums.USER_MUL_EXIST.getCode(),
                    ResponseCodeEnums.USER_MUL_EXIST.getInfo());
        }
        if (userVoList.isEmpty()) {
            return ResponseVo.error(ResponseCodeEnums.USER_NOT_EXIST.getCode(),
                    ResponseCodeEnums.USER_NOT_EXIST.getInfo());
        }
        if (!loginDto.getLoginPwd().equalsIgnoreCase(userVoList.get(0).getPassword())) {
            return ResponseVo.error(ResponseCodeEnums.USER_PASSWORD_ERROR.getCode(),
                    ResponseCodeEnums.USER_PASSWORD_ERROR.getInfo());
        }
        UserVo userVo = userVoList.get(0);
        LoginVo loginVo = LoginVo.builder()
                .loginId(userVo.getUserId())
                .account(userVo.getAccount())
                .phone(userVo.getPhone())
                .email(userVo.getEmail())
                .token(SnowUtils.nextId())
                .build();
        return ResponseVo.success(loginVo);
    }

    @RequestMapping("get-account-info")
    public ResponseVo<LoginVo> getAccountInfo(@RequestBody LoginDto loginDto) {
        LoginVo loginVo = LoginVo.builder().loginId(SnowUtils.nextId()).build();
        return ResponseVo.success(loginVo);
    }
}
